// 本地存储工具类
const storage = {
    // 设置存储
    set(key, value) {
        try {
            uni.setStorageSync(key, value);
        } catch (e) {
            console.error('存储失败:', e);
        }
    },

    // 获取存储
    get(key, defaultValue = null) {
        try {
            const value = uni.getStorageSync(key);
            return value || defaultValue;
        } catch (e) {
            console.error('获取存储失败:', e);
            return defaultValue;
        }
    },

    // 删除存储
    remove(key) {
        try {
            uni.removeStorageSync(key);
        } catch (e) {
            console.error('删除存储失败:', e);
        }
    },

    // 清空所有存储
    clear() {
        try {
            uni.clearStorageSync();
        } catch (e) {
            console.error('清空存储失败:', e);
        }
    },

    // 用户相关存储
    setToken(token) {
        this.set('token', token);
    },

    getToken() {
        return this.get('token');
    },

    setUserInfo(userInfo) {
        this.set('userInfo', userInfo);
    },

    getUserInfo() {
        return this.get('userInfo');
    },

    // 门店相关存储
    setCurrentStoreId(storeId) {
        this.set('currentStoreId', storeId);
    },

    getCurrentStoreId() {
        return this.get('currentStoreId');
    },

    setCurrentStoreName(storeName) {
        this.set('currentStoreName', storeName);
    },

    getCurrentStoreName() {
        return this.get('currentStoreName');
    },

    // 购物车相关存储
    setCartData(cartData) {
        this.set('cartData', cartData);
    },

    getCartData() {
        return this.get('cartData', []);
    },

    // 搜索历史
    setSearchHistory(history) {
        this.set('searchHistory', history);
    },

    getSearchHistory() {
        return this.get('searchHistory', []);
    },

    addSearchHistory(keyword) {
        const history = this.getSearchHistory();
        if (!history.includes(keyword)) {
            history.unshift(keyword);
            if (history.length > 10) {
                history.pop();
            }
            this.setSearchHistory(history);
        }
    },

    // 清除用户数据
    clearUserData() {
        this.remove('token');
        this.remove('userInfo');
    }
};

export default storage; 